﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Crosscutting.Authorize
{
    public class MvcRolePermissionService : IMvcRolePermissionService
    {
        Domain.Uow.IRepository<Domain.Entity.MvcRolePermission> rolePermissionRepository;

        Domain.Uow.IRepository<Domain.Entity.MvcController> controllerRepository;

        Domain.Uow.IRepository<Domain.Entity.MvcControllerClass> controllerClassRepository;

        Domain.Uow.IRepository<Domain.Entity.MvcAction> actionRepository;

        Domain.Uow.IRepository<Domain.Entity.MvcActionClass> actionClassRepository;

        public MvcRolePermissionService(Domain.Uow.IRepository<Domain.Entity.MvcRolePermission> permission,
            Domain.Uow.IRepository<Domain.Entity.MvcController> controller,
            Domain.Uow.IRepository<Domain.Entity.MvcControllerClass> controllerClass,
            Domain.Uow.IRepository<Domain.Entity.MvcAction> action,
            Domain.Uow.IRepository<Domain.Entity.MvcActionClass> actionClass)
        {
            if (permission == null)
            {
                throw new ArgumentNullException("rolePermissionRepository is null");
            }
            if (controller == null)
            {
                throw new ArgumentNullException("controllerRepository is null");

            }
            if (controllerClass == null)
            {
                throw new ArgumentNullException("controllerClassRepository is null");
            }
            if (action == null)
            {
                throw new ArgumentNullException("actionRepository is null");
            }
            if (actionClass == null)
            {
                throw new ArgumentNullException("actionClassRepository is null");
            }
            rolePermissionRepository = permission;
            controllerRepository = controller;
            controllerClassRepository = controllerClass;
            actionRepository = action;
            actionClassRepository = actionClass;
        }

        public Domain.Entity.MvcRolePermission Add(Domain.Entity.MvcRolePermission per, Domain.Entity.User operUser)
        {
            rolePermissionRepository.Add(per);
            rolePermissionRepository.Uow.Commit();
            return per;
        }

        public bool Delete(Domain.Entity.MvcRolePermission per, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                per.State = false;
                rolePermissionRepository.Modify(per);
                rolePermissionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.MvcRolePermission per, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                rolePermissionRepository.Delete(per);
                rolePermissionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User operUser)
        {
            var delete = rolePermissionRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                item.State = false;
                rolePermissionRepository.Modify(item);
            }
            var res = false;
            try
            {
                rolePermissionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User operUser)
        {
            var delete = rolePermissionRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                rolePermissionRepository.Delete(item);
            }
            var res = false;
            try
            {
                rolePermissionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.MvcRolePermission per, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                rolePermissionRepository.Modify(per);
                rolePermissionRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public IQueryable<Domain.Entity.MvcRolePermission> GetRolePermissionByRoleId(int roleId)
        {
            var res = rolePermissionRepository.GetList(d => d.RoleId == roleId).AsQueryable();
            return res;
        }

        public List<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto> GetRoleInstallPermissionByRoleId(int roleId)
        {
            var data = new List<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto>();
            var rolePermissionIdList = rolePermissionRepository.GetList(d => d.RoleId == roleId && d.State == true).Select(e => e.ActionId);
            var permissionList = actionRepository.GetList(e => rolePermissionIdList.Contains(e.Id));
            foreach (var item in permissionList)
            {
                //这里的item就是一个权限
                //启用了的权限
                if (item.State)
                {
                    var action = new Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto();
                    action.ActionId = item.Id;
                    action.ActionName = item.Name;
                    action.ActionClassId = item.ClassId;
                    var actionClassName = actionClassRepository.GetList(e => e.Id == item.ClassId).Select(e => e.Name).FirstOrDefault(); ;
                    action.ActionClassName = string.IsNullOrEmpty(actionClassName) == true ? "权限没有分类" : actionClassName;
                    action.ActionRemark = item.Remark;
                    action.ControllerId = item.ControllerId;
                    var controller = controllerRepository.GetList(e => e.Id == item.ControllerId).FirstOrDefault();
                    if (controller != null)
                    {
                        action.ControllerName = controller.Name;
                        action.ControllerClassId = controller.ClassId;
                        action.ControllerRemark = controller.Remark;
                        var controllerClassName = controllerClassRepository.GetList(e => e.Id == controller.ClassId)
                            .Select(e => e.Name).FirstOrDefault();
                        action.ControllerClassName = string.IsNullOrEmpty(controllerClassName) == true ? "控制器没有分类" : controllerClassName; ;
                    }
                    data.Add(action);
                }
            }
            return data.OrderByDescending(e => e.ActionId).ToList();
        }

        public List<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto> GetRoleUninstallPermissionByRoleId(int roleId)
        {
            var data = new List<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto>();
            var roleInstallPermissionIdList = rolePermissionRepository.GetList(e => e.RoleId == roleId || e.State == false).Select(e => e.ActionId);
            var roleUninstallPermission = actionRepository.GetList(e => !roleInstallPermissionIdList.Contains(e.Id));
            foreach (var item in roleUninstallPermission)
            {
                //这里的item就是一个权限
                //启用了的权限
                if (item.State)
                {
                    var action = new Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto();
                    action.ActionId = item.Id;
                    action.ActionName = item.Name;
                    action.ActionClassId = item.ClassId;
                    var actionClassName = actionClassRepository.GetList(e => e.Id == item.ClassId).Select(e => e.Name).FirstOrDefault(); ;
                    action.ActionClassName = string.IsNullOrEmpty(actionClassName) == true ? "权限没有分类" : actionClassName;
                    action.ActionRemark = item.Remark;
                    action.ControllerId = item.ControllerId;
                    var controller = controllerRepository.GetList(e => e.Id == item.ControllerId).FirstOrDefault();
                    if (controller != null)
                    {
                        action.ControllerName = controller.Name;
                        action.ControllerClassId = controller.ClassId;
                        action.ControllerRemark = controller.Remark;
                        var controllerClassName = controllerClassRepository.GetList(e => e.Id == controller.ClassId)
                            .Select(e => e.Name).FirstOrDefault();
                        action.ControllerClassName = string.IsNullOrEmpty(controllerClassName) == true ? "控制器没有分类" : controllerClassName; ;
                    }
                    data.Add(action);
                }
            }
            return data.OrderByDescending(e => e.ActionId).ToList();
        }


        public IQueryable<Domain.Entity.MvcRolePermission> GetList()
        {
            var res = rolePermissionRepository.GetList().AsQueryable();
            return res;
        }

        public Domain.Entity.MvcRolePermission Get(int id)
        {
            var data = rolePermissionRepository.Get(id);
            return data;
        }
    }
}
